package com.util.sql;


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.util.Properties;

/**
 * 动态代理具体实现方法
 */
public class PropertyInvocationHacdle implements InvocationHandler {
    private Properties properties;
    public PropertyInvocationHacdle(Properties properties){
        this.properties = properties;
    }
    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println(String.format("调用方法【%s】",method.getName()));
        //读取注解
        ReadConf readConf = method.getAnnotation(ReadConf.class);
        if(readConf == null){
            return  null;
        }
        String value = readConf.value();
        //读取配置
        String property = properties.getProperty(value);
        if(property == null){
            return  null;
        }
        //获取参数类型
        Class<?> returnClass = method.getReturnType();
        //基本原始类型
        //判断是否是基本类型
        if(returnClass.isPrimitive()){
            if(returnClass.equals(int.class)){
                //保存为Int型
                return Integer.valueOf(property);
            }
            else if(returnClass.equals(long.class)){
                return Long.valueOf(property);
            }
            else if(returnClass.equals(double.class)){
                return Double.valueOf(property);
            }
            else if (returnClass.equals(float.class)){
                return Float.valueOf(property);
            }
            else if (returnClass.equals(boolean.class)){
                return Boolean.valueOf(property);
            }
        }
        //引用类型
        else{
            if(returnClass.equals(String.class)){
                return String.valueOf(property);
            } else if (returnClass.equals(Integer.class)) {

                return Integer.valueOf(property);
            }else if(returnClass.equals(Boolean.class)){
                return Boolean.valueOf(property);
            }

        }
        return property;
    }
}
